import React from 'react';
import PropTypes from 'prop-types';
import { Redirect, Switch } from 'dva/router';
import { getRoutes } from '../utils/utils';
import AuthorizedRoute from '../components/Authorized/AuthorizedRoute';
import { getMenuData } from '../common/menu';
/**
 * 根据菜单取得重定向地址.
 */
const redirectData = [];
const getRedirect = item => {
  if (item && item.children) {
    if (item.children[0] && item.children[0].path) {
      redirectData.push({
        from: `${item.path}`,
        to: `${item.children[0].path}`,
      });
      item.children.forEach(children => {
        getRedirect(children);
      });
    }
  }
};
getMenuData().forEach(getRedirect);


class BasicLayout extends React.PureComponent{
  render(){
    const {
      routerData,
      match,
    } = this.props;
    return(
      <div>
        <Switch>
          {redirectData.map(item => (
            <Redirect key={item.from} exact from={item.from} to={item.to} />
          ))}
          {getRoutes(match.path, routerData).map(item => (
            <AuthorizedRoute
              key={item.key}
              path={item.path}
              component={item.component}
              exact={item.exact}
              authority={item.authority}
            />
          ))}
        </Switch>
      </div>
    )
  }
}

export default BasicLayout;

